package vn.com.demand.lux.modifier;

import java.util.ArrayList;
import java.util.Date;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.swt.widgets.TableItem;

import vn.com.demand.lux.model.ServiceCardBean;
import vn.com.demand.lux.view.ServiceCardEditor;
import vn.com.demand.util.DpUtils;

public class ServiceCardCellModifier implements ICellModifier {
	
	private ServiceCardEditor serviceCardEditor;
	
	public ServiceCardCellModifier(ServiceCardEditor editor) {
		super();
		serviceCardEditor = editor;
	}
	
	
	public boolean canModify(Object element, String property) {
		return true;
	}

	public Object getValue(Object element, String property) {
//		int index = serviceCardEditor.getColumnNames().indexOf(property);
		int index = 0;
		Object result = null;
		ServiceCardBean input = (ServiceCardBean)element;
		switch (index) {
		case 0:
			//Card number
			result = DpUtils.trim(input.getCardNumber());
			break;
		case 1:
			//Resource ID
			result = DpUtils.trim(input.getResourceId());
			break;	
		case 2:
			//Date
			result = input.getDate();
			break;	
		case 3:
			//From
			result = DpUtils.trim(input.getFromTime());
			break;	
		case 4:
			//To
			result = DpUtils.trim(input.getToTime());
			break;		
		case 5:
			//Customer code
			result = DpUtils.trim(input.getCustomerCode());
			break;
		case 6:
			//Customer name
			result = DpUtils.trim(input.getCustomerName());
			break;			
		case 7:
			//Contract number
			result = DpUtils.trim(input.getContractNumber());
			break;	
		case 8:
			//Service detail
			result = DpUtils.trim(input.getServiceDetail());
			break;		
		case 9:
			//Type
			result = DpUtils.trim(input.getType());
			break;			
		default:
			break;
		}
		return result;
	}

	public void modify(Object element, String property, Object value) {
		try {
			// Find the index of the column 
//			int columnIndex	= serviceCardEditor.getColumnNames().indexOf(property);
			int columnIndex	= 0;
			TableItem item = (TableItem) element;
			ServiceCardBean input = (ServiceCardBean) item.getData();
			String valueString;
			switch (columnIndex) {
				case 0 : // Card number
					valueString = ((String) value).trim();
				    input.setCardNumber(valueString);
					break;
				case 1 : // Resource ID 
					valueString = ((String) value).trim();
					input.setResourceId(valueString);
					break;
				case 2 : // Date
					Date date = (Date) value;
					input.setDate(date);
					break;
				case 3 : // From
					valueString = ((String) value).trim();
					input.setFromTime(valueString);
					break;
				case 4 : // To
					valueString = ((String) value).trim();
					input.setToTime(valueString);
					break;	
				case 5 : // Customer code
					valueString = ((String) value).trim();
					input.setCustomerCode(valueString);
					break; 	
				case 6 : // Customer name
					valueString = ((String) value).trim();
					input.setCustomerName(valueString);
					break;	
				case 7 : // Contract number
					valueString = ((String) value).trim();
					input.setContractNumber(valueString);
					break;	
				case 8 : // Service detail
					valueString = ((String) value).trim();
					input.setServiceDetail(valueString);
					break;	
				case 9 : // Type
					valueString = ((String) value).trim();
					input.setType(valueString);
					break;	
				default :
					break;
				}
//			serviceCardEditor.getTableViewer().update(input, null);
			serviceCardEditor.setDirty(true);
			ArrayList<ServiceCardBean> list = serviceCardEditor.getUpdateList();
			if (!list.contains(input)) {
				list.add(input);
			}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

}
